package com.zxn.project.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zxn.project.mapper.UserGroupMapper;
import com.zxn.project.pojo.*;
import com.zxn.project.service.GroupService;
import com.zxn.project.service.HealthCodeService;
import com.zxn.project.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/manager")
public class ManagerController {
    @Autowired
    private GroupService groupService;
    @Autowired
    UserGroupMapper userGroupMapper;
    @Autowired
    HealthCodeService healthCodeService;


    //进入加载用户管理的所有组
    @GetMapping("/list")
    public R getAllGroup(@RequestHeader(value = "token") String token) throws Exception {
        String openid= JSON.parseObject(JwtUtil.parseJWT(token).getSubject()).get("openid").toString();
        List<Group> list = groupService.list(new LambdaQueryWrapper<Group>().eq(Group::getUid, openid));
        return R.success(list);
    }
    // 根据组id查看组内所有人员
    @GetMapping("/user")
    public R getAllUser(Long groupId)  {
        List<User> list = userGroupMapper.selectUserByGroupID(groupId);
        return R.success(list);
    }
    @GetMapping("/code")
    public R getCodeByOpenid(Long userid) throws Exception {
        LambdaQueryWrapper<HealthCode> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(HealthCode::getId,userid);
        List<HealthCode> list = healthCodeService.list(lambdaQueryWrapper);
        return R.success(list);
    }

    //创建群组
    @PostMapping("/creat")
    public R createGroup(@RequestBody Group group,@RequestHeader(value = "token") String token) throws Exception {
        String openid= JSON.parseObject(JwtUtil.parseJWT(token).getSubject()).get("openid").toString();
        groupService.save(group);
        return R.success("创建成功");
    }
    @DeleteMapping("/disband")
    public R disbandGroup(@RequestBody Group group,@RequestHeader(value = "token") String token) throws Exception {
        groupService.removeById(group.getId());
        return R.success("解散成功");
    }
}
